<template>
  <div class="box">
    <div class="login-inner">
      <h3>小虎外卖管理系统</h3>
      <div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
          size="mini"
        >
          <el-form-item prop="uname">
            <el-input
              v-model="ruleForm.uname"
              prefix-icon="iconfont icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="pass">
            <el-input
              :type="hiddenPass ? 'password' : 'text'"
              v-model="ruleForm.pass"
              prefix-icon="iconfont icon-hide"
              autocomplete="off"
            >
              <template #suffix>
                <i
                  class="iconfont pass-test8
"
                  :class="hiddenPass ? 'icon-test8' : 'icon-browse'"
                  @click="hiddenPass = !hiddenPass"
                ></i> </template
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { Login } from '../api/users.js'
import local from '../utils/local.js'
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pass: '',
        uname: ''
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        uname: [{ validator: checkName, trigger: 'blur' }]
      },
      hiddenPass: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { code, msg, token } = await Login({
            account: this.ruleForm.uname,
            password: this.ruleForm.pass
          })
          // console.log(data)
          if (code === 0) {
            this.$message({
              message: msg,
              type: 'success'
            })
            local.set('token', token)
            setTimeout(() => {
              this.$router.push('/')
            }, 1500)
          } else {
            this.$message.error(msg)
          }
        } else {
          this.$message.error('请输入')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.box {
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}
.login-inner {
  width: 340px;
  height: 240px;
  h3 {
    text-align: center;
    padding-bottom: 10px;
  }
}
</style>
